package com.example.androidcompletesec.xmlparser;

import java.io.FileOutputStream;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.androidcompletesec.R;

public class MainXml extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainxml);
		new  XmlJsonMake().execute();
	}
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.dom:
			startActivity(new Intent(this, Dom_parserActivity.class));
			break;
		case R.id.sax:
			startActivity(new Intent(this, Sax_parserActivity.class));
			break;
		case R.id.pull:
			startActivity(new Intent(this, Pull_parserActivity.class));
			break;
		case R.id.json:
			startActivity(new Intent(this, Json_parserActivity.class));
			break;
		}
	}
	private class XmlJsonMake extends android.os.AsyncTask<Void, Void, Void> {
		ProgressDialog progressDlg;
		
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(MainXml.this, "Wait",
					"order.xml & order.json Making ...");
		}
		protected void onPostExecute(Void result) {
			progressDlg.dismiss();
			Toast.makeText(MainXml.this, "order.xml & order.json make",
					Toast.LENGTH_LONG).show();
		}
		
		protected Void doInBackground(Void... strs) {
			FileOutputStream fos = null;
			try {
				fos = openFileOutput("order.xml", MODE_PRIVATE);
				String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
						+ "<order>\n"
						+ " <item Maker=\"Samsung\" Price=\"23000\">Mouse</item>\n"
						+ " <item Maker=\"LG\" Price=\"12000\">KeyBoard</item>\n"
						+ " <item Price=\"156000\" Maker=\"Western Digital\">HDD</item>\n"
						+ "</order>";
				fos.write(str.getBytes());
				fos.close();
				
				fos = openFileOutput("order.json", MODE_PRIVATE);
				String Json = "[\n"
						+ "{\"Product\":\"Mouse\", \"Maker\":\"Samsung\", \"Price\":23000},\n"
						+ "{\"Product\":\"KeyBoard\", \"Maker\":\"LG\", \"Price\":12000},\n"
						+ "{\"Product\":\"HDD\", \"Maker\":\"Western Digital\", \"Price\":156000} \n"
						+ "]";
				fos.write(Json.getBytes());
				fos.close();
				Thread.sleep(3000);
			} catch (Exception e) {
				Log.d("XmlParser", e.getMessage());
			}
			return null;
		}
	}
}
